/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.hostess.persistence;

import br.hostess.model.Evento;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcelo Daibert
 */
public class EventoDAO {

    public boolean salvar(Evento pEvento) {
        System.out.println("Persistindo Objeto: " + pEvento.toString());
        Connection objConexao = FabricaConexao.getConexao();
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.execute("INSERT INTO tbl_evento(oid, descricao) VALUES('" + pEvento.getOid() + "','" + pEvento.getDescricao() + "')");
            objSTM.close();
            return true;
        } catch (Exception erro) {
            String errorMsg = "Erro ao Persistir: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
            return false;
        }

    }
    public static ArrayList<Evento> recuperaObjetos() {
        Connection objConexao = FabricaConexao.getConexao();
        ArrayList<Evento> vetEventos = new ArrayList<Evento>();
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.executeQuery("SELECT oid, descricao FROM tbl_evento");
            ResultSet objResultSet = objSTM.getResultSet();
            Evento objEventoBuffer;
            while (objResultSet.next()) {
                String vID = objResultSet.getString("oid");
                String vDescricao = objResultSet.getString("descricao");
                objEventoBuffer = new Evento();
                objEventoBuffer.setOid(vID);
                objEventoBuffer.setDescricao(vDescricao);
                vetEventos.add(objEventoBuffer);
            }
            objResultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            String errorMsg = "Erro ao Recuperar: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
        }
        return vetEventos;
    }
    public static boolean excluir(String pOID) {
        System.out.println("Excluindo Objeto: " + pOID);
        Connection objConexao = FabricaConexao.getConexao();
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.execute("DELETE FROM tbl_evento WHERE oid = '"+pOID+"'");
            objSTM.close();
            return true;
        } catch (Exception erro) {
            String errorMsg = "Erro ao Deletar: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
            return false;
        }  
    }
    public static Evento recuperaObjeto(String pID) {
        Connection objConexao = FabricaConexao.getConexao();
        Evento objBuffer = null;
        try {
            Statement objSTM = objConexao.createStatement();
            objSTM.executeQuery("SELECT oid, descricao FROM tbl_evento WHERE oid = '"+pID+"'");
            ResultSet objResultSet = objSTM.getResultSet();
            while (objResultSet.next()) {
                String vID = objResultSet.getString("oid");
                String vDescricao = objResultSet.getString("descricao");
                objBuffer = new Evento();
                objBuffer.setOid(vID);
                objBuffer.setDescricao(vDescricao);
            }
            objResultSet.close();
            objSTM.close();
        } catch (Exception erro) {
            String errorMsg = "Erro ao Recuperar: " + erro.getMessage();
            //JOptionPane.showMessageDialog(null, errorMsg, "Mensagem", JOptionPane.ERROR_MESSAGE);
        }
        return objBuffer;
    }

}
